package com.woniu.serverdriveruser.service;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.internalcommon.constant.CommonStatusEnum;
import com.woniu.internalcommon.constant.DriverCarConstants;
import com.woniu.internalcommon.dto.DriverUser;
import com.woniu.internalcommon.dto.ResponseResult;
import com.woniu.serverdriveruser.mapper.DriverCarBindingMapper;
import com.woniu.internalcommon.dto.DriverCarBindingRelationship;
import com.woniu.serverdriveruser.mapper.DriverMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class DriverCarBindingService {
    @Autowired
    private DriverCarBindingMapper driverCarBindingMapper;

    @Autowired
    private DriverMapper driverMapper;

    /**
     * 根据司机id和车辆id查询绑定关系
     * @param driverCarBindingRelationship
     * @return
     */
    @SentinelResource("getDriverCarBindingRelationships")//这是在sentinel控制台进行配置
    public ResponseResult<DriverCarBindingRelationship>  getDriverCarBindingRelationships(DriverCarBindingRelationship driverCarBindingRelationship){
        log.info("当前绑订关系对象：{}",driverCarBindingRelationship);
        Map<String,Object> map = new HashMap<>();
        map.put("driver_id", driverCarBindingRelationship.getDriverId());
        map.put("car_id", driverCarBindingRelationship.getCarId());
        map.put("bind_state", driverCarBindingRelationship.getBindState());
        List<DriverCarBindingRelationship> driverCarBindingRelationships = driverCarBindingMapper.selectByMap(map);
        if(driverCarBindingRelationships.size() > 0){
            return ResponseResult.success(driverCarBindingRelationships.get(0));
        }
        return ResponseResult.fail("用户/汽车 尚未绑定");
    }

    /**
     * 添加绑定关系
     * @param driverCarBindingRelationship
     */
    public ResponseResult binding(DriverCarBindingRelationship driverCarBindingRelationship){
        // 判断，如果参数中的车辆和司机，已经做过绑定，则不允许再次绑定
        QueryWrapper<DriverCarBindingRelationship> queryWrapper;

        // 判断，如果参数中的司机，已经做过绑定，则不允许再次绑定
        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("driver_id", driverCarBindingRelationship.getDriverId());
        queryWrapper.eq("bind_state", DriverCarConstants.DRIVER_CAR_BIND);
        Long l = driverCarBindingMapper.selectCount(queryWrapper);
        if(l > 0){
            return ResponseResult.fail("用户已经绑定车辆");
        }

        //判断，如果参数中的车辆，已经做过绑定，则不允许再次绑定
        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("car_id", driverCarBindingRelationship.getCarId());
        queryWrapper.eq("bind_state", DriverCarConstants.DRIVER_CAR_BIND);
        l = driverCarBindingMapper.selectCount(queryWrapper);
        if(l > 0){
            return ResponseResult.fail("车辆已经绑定用户");
        }

        LocalDateTime now = LocalDateTime.now();
        driverCarBindingRelationship.setBindingTime(now);
        driverCarBindingRelationship.setBindState(DriverCarConstants.DRIVER_CAR_BIND);

        driverCarBindingMapper.insert(driverCarBindingRelationship);
        return ResponseResult.success("绑定成功");
    }

    /**
     * 解绑
     * @param driverCarBindingRelationship
     */
    public ResponseResult unBinding(DriverCarBindingRelationship driverCarBindingRelationship ){
        // 解绑前先判断--用户和车辆绑定关系是否存在
        ResponseResult<DriverCarBindingRelationship> driverCarBindingRelationships = getDriverCarBindingRelationships(driverCarBindingRelationship);
        if(driverCarBindingRelationships.getCode() == 0){
            return ResponseResult.fail(CommonStatusEnum.DRIVER_CAR_BIND_NOT_EXISTS.getCode(), CommonStatusEnum.DRIVER_CAR_BIND_NOT_EXISTS.getValue());
        }

        LocalDateTime now = LocalDateTime.now();
        driverCarBindingRelationship.setBindingTime(now);
        driverCarBindingRelationship.setBindState(DriverCarConstants.DRIVER_CAR_UNBIND);
        driverCarBindingMapper.updateById(driverCarBindingRelationship);
        return ResponseResult.success("解绑成功");
    }

    /**
     *  根据司机手机号查询司机和车辆绑定关系
     * @param driverPhone
     * @return
     */
    public ResponseResult<DriverCarBindingRelationship> getDriverCarRelationShipByDriverPhone(@RequestParam String driverPhone){
        QueryWrapper<DriverUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("driver_phone",driverPhone);

        DriverUser driverUser = driverMapper.selectOne(queryWrapper);
        Long driverId = driverUser.getId();

        QueryWrapper<DriverCarBindingRelationship> driverCarBindingRelationshipQueryWrapper = new QueryWrapper<>();
        driverCarBindingRelationshipQueryWrapper.eq("driver_id",driverId);
        driverCarBindingRelationshipQueryWrapper.eq("bind_state", DriverCarConstants.DRIVER_CAR_BIND);

        DriverCarBindingRelationship driverCarBindingRelationship = driverCarBindingMapper.selectOne(driverCarBindingRelationshipQueryWrapper);
        return ResponseResult.success(driverCarBindingRelationship);

    }
}
